#include "eproject.h"
#include<QFile>
#include<QXmlStreamReader>
#include<QMessageBox>
#include<QFileInfo>

EProject::EProject(QTreeWidget* parent)
{
    m_folderIcon.addPixmap(QPixmap(QString::fromUtf8(":/icons/res/icons/folder-open-document.png")), QIcon::Normal, QIcon::Off);
    m_applicationIcon.addPixmap(QPixmap(QString::fromUtf8(":/icons/res/icons/asterisk.png")), QIcon::Normal, QIcon::Off);
    m_headerIcon.addPixmap(QPixmap(QString::fromUtf8(":/icons/res/icons/document-attribute-h.png")), QIcon::Normal, QIcon::Off);
    m_sourceIcon.addPixmap(QPixmap(QString::fromUtf8(":/icons/res/icons/document-attribute-c.png")), QIcon::Normal, QIcon::Off);
    m_undefinedIcon.addPixmap(QPixmap(QString::fromUtf8(":/icons/res/icons/document.png")), QIcon::Normal, QIcon::Off);
    m_author = "Edward";
    m_id = QString::number(-1,10);
    m_name = "Untitled Project";
    m_tree = new QTreeWidgetItem(parent);
    m_tree->setIcon(0,QIcon(QPixmap(QString::fromUtf8(":/icons/res/icons/home.png"))));
    QFont font= m_tree->font(0);
    font.setBold(true);
    m_tree->setFont(0,font);
    m_hasError = false;
}

void EProject::open(QString projectFile)
{
    QTreeWidgetItem *curr = m_tree;
    this->m_hasError = true;
    QFile file(projectFile);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QMessageBox::critical(0, QString("Error in opening project file"), file.errorString());
    }
    if(file.isReadable())
    {
        QXmlStreamReader reader(file.readAll());
        QFileInfo fInfo(file);
        QString currPath = fInfo.absolutePath();
        while(!reader.atEnd())
        {
            reader.readNext();
            if(reader.name().compare("id")==0)
            {
                m_id = reader.readElementText();
            }
            else if(reader.name().compare("name")==0)
            {
                m_name = reader.readElementText();
                m_tree->setText(0,m_name);
                m_tree->setText(1,fInfo.absoluteFilePath());
                m_tree->setText(2,"Project");
                m_tree->setText(3,"Project description");
            }
            else if(reader.name().compare("author")==0)
            {
                m_name = reader.readElementText();
            }
            else if(reader.name().compare("file")==0)
            {
                QString ref = reader.readElementText();
                QStringList info= QStringList(ref);
                info<<currPath<<"UNKNOWN"<<"description";
                QTreeWidgetItem *child = new QTreeWidgetItem(curr, info);
                QFileInfo nfo(currPath+"/"+ref);
                if(nfo.suffix().compare("h") == 0)
                {
                    child->setIcon(0,m_headerIcon);
                    child->setText(2, "Header");
                }
                else if(nfo.suffix().compare("cpp") == 0 || nfo.suffix().compare("c") == 0)
                {
                    child->setIcon(0,m_sourceIcon);
                    child->setText(2, "Source");
                }
                else if(nfo.suffix().compare("exe") == 0)
                {
                    child->setIcon(0,m_applicationIcon);
                    child->setText(2, "Application");
                }
                else
                {
                    child->setIcon(0,m_undefinedIcon);
                    child->setText(2, "Unknown");
                }
                curr->addChild(child);
            }
            else if(reader.name().compare("folder") ==0)
            {
                if(reader.isStartElement())
                {
                    QString ref = reader.attributes().value("name").toString();
                    QStringList info= QStringList(ref);
                    info<<currPath<<"Folder"<<"description";
                    QTreeWidgetItem *fld = new QTreeWidgetItem(curr,info);
                    fld->setIcon(0,m_folderIcon);
                    curr->addChild(fld);
                    curr = fld;
                    currPath += "/";
                    currPath += reader.attributes().value("name").toString();
                }
                else
                {
                    curr = curr->parent();
                    int beg = currPath.lastIndexOf("/");
                    currPath.remove(beg, currPath.size()-beg);
                }
            }

        }
        this->m_hasError = false;
        return;
    }
    else
        qDebug("Error@:(");
}
